package com.tegistuff.webos.run;

import com.intellij.execution.ExecutionException;
import com.intellij.execution.ExecutionManager;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.process.OSProcessHandler;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.tegistuff.webos.AppComponent;

import java.io.File;

/**
 * WebOSRunner.java
 *
 * Copyright (C) 2012 Timo Tegtmeier.
 * Copyright (C) 2011 Janni Kovacs.
 * All rights reserved.
 */
public class WebOSRunner {

    public static final Key<WebOSRunConfiguration> WEB_OS_RUN_CONFIGURATION_KEY = Key.create("WebOSRunConfiguration");

    private boolean appAlreadyRunning;
    private OSProcessHandler processHandler;

    public WebOSRunner(Project project,
                       WebOSRunConfiguration config,
                       BuildMode buildMode) throws ExecutionException
    {
        VirtualFile appFolder = config.getAppFolder();
        RunTarget target = config.getTarget();

        if (appFolder == null)
            throw new ExecutionException("Please select a valid webOS application directory");

        ProcessHandler[] runningProcesses = ExecutionManager.getInstance(project).getRunningProcesses();
        for (ProcessHandler process : runningProcesses) {
            if (!process.isProcessTerminated()) {
                WebOSRunConfiguration data = process.getUserData(WEB_OS_RUN_CONFIGURATION_KEY);
                if (data != null) {
                    if (data.equals(config)) {
                        appAlreadyRunning = true;
                    }
                }
            }
        }

        IdeaPluginDescriptor descriptor = PluginManager.getPlugin(PluginId.getId(AppComponent.PLUGIN_ID));
        File palmRunJar = new File(descriptor.getPath(), "lib/palm_run.jar");

        GeneralCommandLine command = new GeneralCommandLine();
        command.setExePath("java");
        command.addParameter("-cp");
        command.addParameter(AppComponent.getWebOSToolsJarPath() + File.pathSeparatorChar + palmRunJar.getAbsolutePath());
        command.addParameter("com.tegistuff.palmrun.Main");
        command.addParameter(target.getId());
        command.addParameter(appFolder.getPath());

        if(config.getPkgFolder() != null)
            command.addParameter("pkg=" + config.getPkgFolder().getPath());
        if(config.getSrvFolder() != null)
            command.addParameter("srv=" + config.getSrvFolder().getPath());
        if(config.getAccFolder() != null)
            command.addParameter("acc=" + config.getAccFolder().getPath());
        if(config.getOutputFolder() != null)
            command.addParameter("output=" + config.getOutputFolder());

        command.addParameter("mode=" + buildMode.toString());
        command.addParameter("use-v1=" + config.UseV1Format);

        String configName = config.getName();
        String title = "Run '" + configName + "' on " + target;

        processHandler = new OSProcessHandler(command.createProcess(), title);
        processHandler.putUserData(WEB_OS_RUN_CONFIGURATION_KEY, config);
    }

    public boolean getIsAppAlreadyRunning() {
        return appAlreadyRunning;
    }

    public OSProcessHandler getProcessHandler() {
        return processHandler;
    }

    public enum BuildMode {
        BUILD_ONLY,
        BUILD_AND_RUN,
        BUILD_RUN_LOG,
        LOG_ONLY
    }
}
